﻿using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Web.UI.WebControls;

using Sunyi.Project.WebPoint.BusinessLogic;
using Sunyi.Project.WebPoint.Common;
using Sunyi.Project.WebPoint.DataContract;
using System.Text;

namespace ActivitySystem.Web
{
    public static class WebPointWebCommon
    {
        internal const string UrlAdd = "+";
        internal const string UrlAddReplace = "%2B";
        internal const string RedirectUrlFormatRead = "{0}?{1}={2}&Mode=Read";

        public static void SetItemSelected(ListControl control, string value)
        {
            foreach (ListItem item in control.Items)
            {
                if (item.Value == value)
                {
                    item.Selected = true;
                    break;
                }
            }
        }

        public static void HandleAttachments(Collection<Attachment> attachments, Collection<Guid> attachmentIds)
        {
            AttachmentManager attachmentManager = new AttachmentManager();

            Collection<Guid> removedAttachmentIds = new Collection<Guid>();
            foreach (var attachmentId in attachmentIds)
            {
                // Remove the attachment records which have been removed.
                bool flag = false;
                foreach (var attachment in attachments)
                {
                    if (attachment.Id == attachmentId)
                    {
                        flag = true; break;
                    }
                }

                if (!flag) removedAttachmentIds.Add(attachmentId);
            }

            foreach (var attachmentId in removedAttachmentIds)
            {
                attachmentManager.Delete(attachmentId);
                attachmentIds.Remove(attachmentId);
            }

            foreach (var attachment in attachments)
            {
                if (!attachmentIds.Contains(attachment.Id))
                {
                    // Save attachment record into database.
                    attachmentManager.Insert(attachment);
                    // Add attachment record to attachment collection of referring entity.
                    attachmentIds.Add(attachment.Id);

                    // Move attachment from temp folder to upload folder.
                    string tempFilePath = EnvironmentSetting.UploadTempFolderPath + attachment.AttachmentName;
                    string filePath = EnvironmentSetting.UploadFolderPath + attachment.AttachmentName;
                    if (File.Exists(tempFilePath)) File.Move(tempFilePath, filePath);
                }
            }
        }

        public static string EncodeAccessCode(string accessCode)
        {
            var sb = new StringBuilder();
            for (int i = 0; i < accessCode.Length; i++)
            {
                sb.Append((int)accessCode[i]).ToString();
                if (i < accessCode.Length - 1) sb.Append(WebPointGlobalConstant.Dash);
            }

            return sb.ToString();
        }

        public static string DecodeAccessCode(string accessCode)
        {
            var sb = new StringBuilder();
            string[] numbers = accessCode.Split(WebPointGlobalConstant.Dash[0]);

            for (int i = 0; i < numbers.Length; i++)
            {
                sb.Append(((char)Int32.Parse(numbers[i])).ToString());
            }

            return sb.ToString();
        }
    }
}